<?php
namespace Admin\Controller;
use Admin\Logic\ArticleCatLogic;

class SelfController extends BaseController {
    public function index(){
      $act_list = session('act_list');
      $menu_list = getMenuList($act_list);
      $this->assign('menu_list',$menu_list);
      $admin_info = getAdminInfo(session('admin_id'));
      $this->assign('admin_info',$admin_info);
      //top
      $first_column=M('first_column');
      $first_column=$first_column->select();
      $this->assign('first_column',$first_column);

      if($admin_info['jurisdiction'] == 19){
        $this->assign('checkadmin',"style='display:block;'");
      }else{
        $this->assign('checkadmin',"style='display:none;'");
      }

      if($admin_info['jurisdiction'] > 10 || $admin_info['jurisdiction'] == 5){
          $this->assign('checkeditor',"style='display:block;'");
        }else{
          $this->assign('checkeditor',"style='display:none;'");
        }

      if($admin_info['jurisdiction'] == 15 || $admin_info['jurisdiction'] == 19){
          $this->assign('checkseo',"style='display:block;'");
        }else{
          $this->assign('checkseo',"style='display:none;'");
        }

        $this->assign("self","style='display:block;'");
        $this->display();
      }


    /**
     * 初始化编辑器链接
     * @param $post_id post_id
     */
    private function initEditor()
    {
        $this->assign("URL_upload", U('Admin/Ueditor/imageUp',array('savepath'=>'article')));
        $this->assign("URL_fileUp", U('Admin/Ueditor/fileUp',array('savepath'=>'article')));
        $this->assign("URL_scrawlUp", U('Admin/Ueditor/scrawlUp',array('savepath'=>'article')));
        $this->assign("URL_getRemoteImage", U('Admin/Ueditor/getRemoteImage',array('savepath'=>'article')));
        $this->assign("URL_imageManager", U('Admin/Ueditor/imageManager',array('savepath'=>'article')));
        $this->assign("URL_imageUp", U('Admin/Ueditor/imageUp',array('savepath'=>'article')));
        $this->assign("URL_getMovie", U('Admin/Ueditor/getMovie',array('savepath'=>'article')));
        $this->assign("URL_Home", "");
    }

    public function add_seo()
    {
        $second_seo=M()->query("select * from `xl_seotitle_two` order by `id` desc limit 1");
        $this->assign("second_seo",$second_seo[0]);
        $this->display();
    }
    public function add_seohandle()
    {
        $data = I('post.');
        $r = D('seotitle_two')->add($data);
        $referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U('Admin/Self/');
        if($r){
            $this->success("操作成功",$referurl);
        }else{
            $this->error("操作失败",$referurl);
        }
    }

    public function add_article(){
        $act_list = session('act_list');
        $menu_list = getMenuList($act_list);
        $this->assign('menu_list',$menu_list);
        $admin_info = getAdminInfo(session('admin_id'));
        $this->assign('admin_info',$admin_info);

        if($admin_info['jurisdiction'] > 10){
          $this->assign('checkadmin',"isadmin");
        }else{
          $this->assign('checkadmin',"isordinary");
        }

        if(isset($_POST['province_id']) && $_POST['province_id'] != ''){
          $parent_id=$_POST['province_id'];
          $city=M('region2');
          $result_city=$city->query("select * FROM `xl_region2` where `parent_id` = '$parent_id'");
          $result_city=json_encode($result_city);
      	  echo $result_city;
        }elseif( isset($_GET['third_column_id']) && $_GET['third_column_id'] !='' ){
          $get_third_id=$_GET['third_column_id'];

          $third_column=M()->query("select * from `xl_third_column` where `id` = '$get_third_id'");
          $fourth_column=M()->query("select * from `xl_fourth_column` where `third_column_id` = '$get_third_id'");

          $first_column_id=$third_column[0]['first_column_id'];
          $first_column_name=M()->query("select `name` from `xl_first_column` where `id` = '$first_column_id'");
          $second_column_id=$third_column[0]['second_column_id'];
          $second_column_name=M()->query("select `name` from `xl_second_column` where `id` = '$second_column_id'");
          $third_column_name=$third_column[0];

          $province=M('region2');
          $province=$province->where('level = 1')->select();

          $time=time();
          $this->assign('third_column',$third_column);
          $this->assign('fourth_column',$fourth_column);
          $this->assign('first_column_name',$first_column_name);
          $this->assign('second_column_name',$second_column_name);
          $this->assign('third_column_name',$third_column_name);
          $this->assign('province',$province);
          $this->assign('time',$time);
          $this->initEditor();
          $this->display();
        }elseif( isset($_GET['second_column_id']) && $_GET['second_column_id'] !='' ){
          $get_second_id=$_GET['second_column_id'];
          $second_column=M()->query("select * from `xl_second_column` where `id` = '$get_second_id'");
          $fourth_column=M()->query("select * from `xl_fourth_column` where `second_column_id` = '$get_second_id'");

          $first_column_id=$second_column[0]['first_column_id'];
          $first_column_name=M()->query("select * from `xl_first_column` where `id` = '$first_column_id'");
          $second_column_name=$second_column;

          $province=M('region2');
          $province=$province->where('level = 1')->select();

          $time=time();
          $third_column_name=array("first_column_id"=>$first_column_id,"second_column_id"=>$second_column_name[0]['id'],"id"=>0);
          $this->assign('third_column',$third_column);
          $this->assign('fourth_column',$fourth_column);
          $this->assign('first_column_name',$first_column_name);
          $this->assign('second_column_name',$second_column_name);
          $this->assign('third_column_name',$third_column_name);
          $this->assign('province',$province);
          $this->assign('time',$time);
          $this->initEditor();
          $this->display();
        }
    }

    public function articleList(){
      if(isset($_POST['searchtitle']) && $_POST['searchtitle'] != ''){
        $second_column_id=$_POST['second_column_id'];
        $this->assign('get_second_column_id',$second_column_id);
        $article =  M('article_xueli');
        $res = array();
        $p = empty($_REQUEST['p']) ? 1 : $_REQUEST['p'];
        $size = empty($_REQUEST['size']) ? 100 : $_REQUEST['size'];
        $where = " 1 = 1 ";
        $searchtitle = trim(I('searchtitle'));
        $where.=" and title like '%$searchtitle%' and second_column_id='$second_column_id'";
        $res = $article->where($where)->order('id desc')->page("$p,$size")->select();
        $count = $article->where($where)->count();// 查询满足要求的总记录数
        $pager = new \Think\Page($count,$size);// 实例化分页类 传入总记录数和每页显示的记录数
        $page = $pager->show();//分页显示输出

        $first_column=M('first_column');
        $first_column_id=$res[0]['first_column_id'];
        $first_column_name=$first_column->query("select `name` from `xl_first_column` where `id` = '$first_column_id'");

        $second_column=M('second_column');
        $second_column_id=$res[0]['second_column_id'];
        $second_column_name=$second_column->query("select `name` from `xl_second_column` where `id` = '$second_column_id'");

        $this->assign('page',$page);// 赋值分页输出
        $this->assign('article',$res);
        $this->assign("first_column_name",$first_column_name);
        $this->assign("second_column_name",$second_column_name);
        $this->display();
       }else{
        $get_second_column_id = $_GET['id'];
        $article =  M('article_xueli');

        $p = empty($_REQUEST['p']) ? 1 : $_REQUEST['p'];
        $size = empty($_REQUEST['size']) ? 20 : $_REQUEST['size'];
        $where = " 1 = 1 ";
        $where.=" and second_column_id='$get_second_column_id'";
        $res = $article->where($where)->order('id desc')->page("$p,$size")->select();
        $count = $article->where($where)->count();// 查询满足要求的总记录数
        $pager = new \Think\Page($count,$size);// 实例化分页类 传入总记录数和每页显示的记录数
        $page = $pager->show();//分页显示输出

        $first_column=M('first_column');
        $first_column_id=$res[0]['first_column_id'];
        $first_column_name=$first_column->query("select `name` from `xl_first_column` where `id` = '$first_column_id'");

        $second_column=M('second_column');
        $second_column_id=$res[0]['second_column_id'];
        $second_column_name=$second_column->query("select `name` from `xl_second_column` where `id` = '$second_column_id'");

        $this->assign('page',$page);// 赋值分页输出
        $this->assign("article",$res);
        $this->assign("first_column_name",$first_column_name);
        $this->assign("second_column_name",$second_column_name);
        $this->assign("get_second_column_id",$get_second_column_id);
        $this->display();
      }
    }

    public function edit_article(){
        $act_list = session('act_list');
        $menu_list = getMenuList($act_list);
        $this->assign('menu_list',$menu_list);
        $admin_info = getAdminInfo(session('admin_id'));
        $this->assign('admin_info',$admin_info);

      if(isset($_GET['uid']) && $_GET['uid'] != ''){
        $province=M('region2');
        $province=$province->where('level = 1')->select();
        $article_id=$_GET['uid'];
        $article=M('article_xueli');
        $article=$article->where("id = '$article_id'")->select();
        $article=$article[0];

        $province_id=$article['province_id'];
        $province_name=M()->query("select * from __PREFIX__region2 where id = '$province_id'");
        $this->assign("province_name",$province_name[0]);

        $city_id=$article['city_id'];
        $city=M()->query("select * from __PREFIX__region2 where id = '$city_id'");
        $this->assign("city",$city[0]);

        $first_column=M('first_column');
        $first_column_id=$article['first_column_id'];
        $first_column_name=$first_column->where("id = '$first_column_id'")->select();

        $second_column=M('second_column');
        $second_column_id=$article['second_column_id'];
        $second_column_name=$second_column->where("id = '$second_column_id'")->select();

        $third_column=M('third_column');
        $third_column_id=$article['third_column_id'];
        $third_column_name=$third_column->where("id = '$third_column_id'")->select();

        $fourth_column_table=M('fourth_column');
        if($third_column_id == 0){
          $fourth_column=M()->query("select * from __PREFIX__fourth_column where `second_column_id` = '$second_column_id'");
          $this->assign("fourth_column",$fourth_column);
        }else{
          $fourth_column=M()->query("select * from __PREFIX__fourth_column where `third_column_id` = '$third_column_id'");
          $this->assign("fourth_column",$fourth_column);
        }

        if($admin_info['jurisdiction'] > 10){
          $this->assign('checkadmin',"isadmin");
        }else{
          $this->assign('checkadmin',"isordinary");
        }

        $this->assign("article",$article);
        $this->assign("province",$province);
        $this->assign("first_column_name",$first_column_name);
        $this->assign("second_column_name",$second_column_name);
        $this->assign("third_column_name",$third_column_name);
        $this->initEditor();
        $this->display();
      }else{
        $this->error("获取失败",U('Admin/Self/'));
      }
    }

    public function articleHandle(){
      $data = I('post.');
      $data['publish_time'] = strtotime($data['publish_time']);
      //$data['content'] = htmlspecialchars(stripslashes($_POST['content']));
      if($data['act'] == 'add'){
  		  $article =  M('article_xueli');
  		  $article_title=$data['title'];
  		  $count = $article->where("`title` = '$article_title'")->count();
  		  if($count == 0){
  			  $data['content'] = str_replace('；', ';',$data['content']);
  			 //$data['click'] = mt_rand(1000,1300);
  			 $r = D('article_xueli')->add($data);
  		  }else{
  			  $r = false;
  		  }
      }

      if($data['act'] == 'edit'){
        $data['content'] = str_replace('；', ';',$data['content']);
        $r = D('article_xueli')->where('id='.$data['article_id'])->save($data);
      }
      //
      if($data['act'] == 'del'){
        $r = D('article_xueli')->where('id='.$data['article_id'])->delete();
        if($r) exit(json_encode(1));
      }

      $referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U('Admin/Self/');
      if($r){
          $this->success("操作成功",$referurl);
      }else{
          $this->error("操作失败",$referurl);
      }
    }
}
